// FrogSettingsDialog.cpp : Implementation of CFrogSettingsDialog

#include "stdafx.h"
#include "FrogSettingsDialog.h"
#include "RecentActionsManager.h"
#include "FilerFrogSettings.h"

// CFrogSettingsDialog

LRESULT CFrogSettingsDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CFrogBaseDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);

	FilerFrogSettings filerFrogSettings;
	bool useDefault = filerFrogSettings.GetFavoritesSettings();

	int daysUpdateInterval = filerFrogSettings.GetCheckForUpdatesSettings();

	HWND comboUpdate = GetDlgItem(IDC_COMBO_UPDATE);

	::SendMessage(comboUpdate,(UINT)CB_ADDSTRING, 0, (LPARAM)L"Day");
	::SendMessage(comboUpdate,(UINT)CB_ADDSTRING, 0, (LPARAM)L"Week");
	::SendMessage(comboUpdate,(UINT)CB_ADDSTRING, 0, (LPARAM)L"Month");
	::SendMessage(comboUpdate,(UINT)CB_ADDSTRING, 0, (LPARAM)L"Never");

	switch (daysUpdateInterval)
	{
	case 1:
		{
			// Selects the first item
			::SendMessage(comboUpdate,(UINT)CB_SETCURSEL, (WPARAM) 0 , 0);
		}
		break;
	case 7:
		{
			// Selects the 2nd item
			::SendMessage(comboUpdate,(UINT)CB_SETCURSEL, (WPARAM) 1 , 0);
		}
		break;
	case 30:
		{
			// Selects the 3rd item
			::SendMessage(comboUpdate,(UINT)CB_SETCURSEL, (WPARAM) 2 , 0);
		}
		break;
	default:
		{
			// Selects the 4rd item
			::SendMessage(comboUpdate,(UINT)CB_SETCURSEL, (WPARAM) 3 , 0);
		}
		break;
	}

	if (useDefault)
	{
		CheckDlgButton(IDC_RADIO_FAV_NORMAL, BST_CHECKED);
		CheckDlgButton(IDC_RADIO_FAV_ALL, BST_UNCHECKED);
	}
	else
	{
		CheckDlgButton(IDC_RADIO_FAV_NORMAL, BST_UNCHECKED);
		CheckDlgButton(IDC_RADIO_FAV_ALL, BST_CHECKED);
	}

	return 0;
}

LRESULT CFrogSettingsDialog::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	bHandled = TRUE;
	// TODO: Add your control notification handler code here

	bool useDefault = true;

	HWND buttonUseDefault = GetDlgItem(IDC_RADIO_FAV_NORMAL);
	LRESULT lResult = ::SendMessage(buttonUseDefault, (UINT)BM_GETCHECK, 0, 0);

	if (lResult != BST_CHECKED)
	{
		useDefault = false;
	}

	HWND comboUpdate = GetDlgItem(IDC_COMBO_UPDATE);
	lResult = SendMessage(comboUpdate,(UINT)CB_GETCURSEL, 0 , 0);

	int daysUpdateInterval = 7;	

	if (lResult != CB_ERR)
	{
		switch (lResult)
		{
		case 0:
			{
				daysUpdateInterval = 1;
			}
			break;
		case 1:
			{
				daysUpdateInterval = 7;
			}
			break;
		case 2:
			{
				daysUpdateInterval = 30;
			}
			break;
		case 3:
			{
				daysUpdateInterval = 0;
			}
			break;
		}
	}

	FilerFrogSettings filerFrogSettings;
	filerFrogSettings.SetFavoritesSettings(useDefault);
	filerFrogSettings.SetCheckForUpdatesSettings(daysUpdateInterval);

	EndDialog(wID);
	return 0;
}

LRESULT CFrogSettingsDialog::OnClickedClear(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	HWND clearButton = GetDlgItem(IDC_BUTTON_CLEAR_RECENT);
	::EnableWindow(clearButton, FALSE);

	RecentActionsManager recentActionsManager;
	recentActionsManager.ClearAndSave();

	return 0;
}
